<template>
	<view class="tui-safe-area">
		<view class="tui-address" v-if="addressLists  .length>0">
			<block v-for="(item,index) in addressLists" :key="index">
				<tui-slide-view :buttons="buttons" @click="handleClick(item)">
					<tui-list-cell padding="0" >
						<view class="tui-address-flex">
							<view class="tui-address-left" @click="checkData(item)">
								<view class="tui-address-main">
									<view class="tui-address-name tui-ellipsis">{{item.nickName}}</view>
									<view class="tui-address-tel">{{item.phoneNum}}</view>
								</view>
								<view class="tui-address-detail">
									<!-- <view class="tui-address-label" v-if="item.isDefault">默认</view> -->
									<!-- <view class="tui-address-label" v-if="index!=2">{{["公司","住宅","其它"][index]}}</view> -->
									<text>{{item.fullAddress}}</text>
								</view>
							</view>
							<view class="tui-address-imgbox" @click="editAddr(item)">
								<image class="tui-address-img" src="/static/icon_addr_edit.png" />
							</view>
						</view>
					</tui-list-cell>
				</tui-slide-view>
			</block>
		</view>
		<tui-no-data v-else imgUrl="/static/images/toast/img_nodata.png">暂无数据</tui-no-data>
		<!-- 新增地址 -->
		<view class="tui-address-new">
			<tui-button shape="circle" type="brown" height="80rpx" @click="editAddr">+ 新增收货地址</tui-button>
		</view>
	</view>
</template>

<script>
	import {
		businessAddress,
		addressList,
		
	} from '@/api/index'
	export default {
		data() {
			return {
				addressLists: [],
				buttons: [{
					text: '删除',
					color: '#fff',
					background: '#FA5151'
				}],
				type: null
			}
		},
		onLoad(options) {
			console.log(options);
			this.type = options.type
			this.getData()
		},
		methods: {
			getData() {
				addressList({
						addressType: this.type,
						openId:uni.getStorageSync('user').openid
					}, 'get')
					.then((res) => {
						if (res.code == 200) {
							this.addressLists = res.rows

						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					})
			},
			checkData(item) {
				// uni.$emit('option', {
				// 	item
				// });
				// setTimeout(() => {
				// 	uni.navigateBack();
				// }, 0);
				if (Number(this.type) ) {
					uni.navigateTo({
						url: `/pages/sendingPage/sendingPage?val=${encodeURIComponent(JSON.stringify(item))}`
					})
				} else {
					uni.navigateTo({
						url: `/pages/collectPage/collectPage?val=${encodeURIComponent(JSON.stringify(item))}`
					})
				}
			},
			editAddr(val) {
				if(	val.id){
					console.log(val.id);
					uni.navigateTo({
						url: `../editAddress/editAddress?id=${val.id}`
					})
				}else{
					console.log(this.type);
					uni.navigateTo({
						url: `../editAddress/editAddress?type=${this.type}`
					})	
				}
				
			},
			handleClick(item) {
				businessAddress({}, 'delete', item.id)
					.then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: '删除成功',
								duration: 2000
							});
							this.getData()
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					})
			},
		}
	}
</script>

<style>
	.tui-address {
		width: 100%;
		padding-top: 20rpx;
		padding-bottom: 160rpx;
	}

	.tui-address-flex {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.tui-address-main {
		height: 70rpx;
		display: flex;
		font-size: 30rpx;
		line-height: 86rpx;
		padding-left: 30rpx;
	}

	.tui-address-name {
		/* width: 120rpx; */
		height: 60rpx;
	}

	.tui-address-tel {
		margin-left: 12rpx;
	}

	.tui-address-detail {
		font-size: 24rpx;
		word-break: break-all;
		padding-bottom: 25rpx;
		padding-left: 25rpx;
		padding-right: 120rpx;
	}

	.tui-address-label {
		padding: 5rpx 8rpx;
		flex-shrink: 0;
		background: #ac9157;
		border-radius: 6rpx;
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
		line-height: 25rpx;
		transform: scale(0.8);
		transform-origin: center center;
		margin-right: 6rpx;
	}

	.tui-address-imgbox {
		width: 80rpx;
		height: 100rpx;
		position: absolute;
		display: flex;
		justify-content: center;
		align-items: center;
		right: 10rpx;
	}

	.tui-address-img {
		width: 36rpx;
		height: 36rpx;
	}

	.tui-address-new {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 999;
		padding: 20rpx 60rpx 30rpx;
		box-sizing: border-box;
		background: #f8f8f8;
	}

	.tui-safe-area {
		padding-bottom: env(safe-area-inset-bottom);
	}
</style>